/**   
 * @Title: LoadingViewHandler.java 
 * @Package com.care.barsearch.handler 
 * @Description: TODO
 * @author Evan 
 * @date Jun 7, 2012 5:02:16 PM 
 * @version V1.0   
 */
package com.evan.common.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.skinapp.R;

public class LoadingProgressExt {

	private static Dialog dlg;
	static TextView alert;

	private static Dialog createDialog(Context context, int msg) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		// main.xml中的ImageView
		ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
		TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
		tipTextView.setVisibility(View.GONE);
		// 加载动画
		Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
				context, R.anim.loading_anim);
		// 使用ImageView显示动画
		spaceshipImage.startAnimation(hyperspaceJumpAnimation);
		tipTextView.setText(msg);// 设置加载信息

		dlg = new Dialog(context, R.style.loading_dialog);
		dlg.show();
		dlg.setCancelable(false);

		// dlg.setCancelable(false);// 不可以用“返回键”取消
		dlg.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
		return dlg;
	}

	public static void dismiss() {
		try {
			if (isShowing())
				dlg.dismiss();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static boolean isShowing() {
		if (dlg != null) {
			return dlg.isShowing();
		}
		return false;
	}

	public static void showProgressDialog(Context context, int textResource) {
		if (dlg == null || !isShowing()) {
			createDialog(context, textResource);
		}
	}

	public static void setMessage(String message) {
		alert.setText(message);
	}
}
